#! /usr/bin/python
# -*- coding: utf-8 -*-
'''
This file is used to format the whole repo's C/C++ source file.

If you have any advises, We are happy to hear from you.


Follow us (http://yunyoo.cn/):
----------------------------------------------------------------------
        Email                           971159199@qq.com
        公众号                           江哥说安全
        视频号                           江哥说安全
        头条                             刘柏江
        抖音                             江哥说安全/GeekNeo
        微博                             刘柏江VM
        码云                             https://gitee.com/geekneo/
----------------------------------------------------------------------
'''
import os
import sys
import subprocess

def paths(path):
    path_collection = []
    for dirpath, dirnames, filenames in os.walk(path):
        for file in filenames:
            fullpath = os.path.join(dirpath, file)
            path_collection.append(fullpath)
    return path_collection
    
clang_format = os.getenv('CLANG_FORMAT')
if clang_format == None or clang_format == '':
    print('You should set the full path of clang format binary with CLANG_FORMAT.')
    sys.exit(-1)

repo_root = os.path.dirname(__file__)
if repo_root == '':
    repo_root = './'

for p in paths(repo_root):
    if p.find('third') > 0:
        continue
    if p.endswith('.c') or \
        p.endswith('cpp') or \
        p.endswith('.cc') or \
        p.endswith('.h') or \
        p.endswith('.hpp') or \
        p.endswith('.m') or \
        p.endswith('.mm'):
            print('Formating "%s" ...' % (p))
            subprocess.Popen([clang_format, '-i', '-style=Google', p]).wait()

sys.exit(0)
